import { defHttp } from '/@/utils/http/axios'
import { LoginParams, LoginResultModel, GetUserInfoModel, VerifyCodeResult } from './model/userModel'
import { ErrorMessageMode, AxiosPromise } from '/#/axios'

enum Api {
  CodeImg = '/auth/code',
  Login = '/auth/login',
  GetUserInfo = '/system/user/getInfo',
  Logout = '/auth/logout',
  GetPermCode = '/getPermCode',
  TestRetry = '/testRetry',
}

// pc端固定客户端授权id
const clientId = import.meta.env.VITE_APP_CLIENT_ID;
/**
 * @description: user login api
 */
export function loginApi(data: LoginParams, mode: ErrorMessageMode = 'modal') {
  const params = {
    ...data,
    clientId: data.clientId || clientId,
    grantType: data.grantType || 'password'
  };
  return defHttp.post<LoginResultModel>(
    {
      url: Api.Login,
      headers: {
        isToken: false,
        isEncrypt: true
      },
      data: params
    },
    {
      errorMessageMode: mode,
    },
  )
}

/**
 * 获取验证码
 */
export function getCodeImg(): AxiosPromise<VerifyCodeResult> {
  return defHttp.get({
    url: Api.CodeImg,
    headers: {
      isToken: false
    },
    timeout: 20000
  });
}

/**
 * @description: getUserInfo
 */
export function getUserInfo() {
  return defHttp.get<GetUserInfoModel>({ url: Api.GetUserInfo }, { errorMessageMode: 'none' })
}

export function getPermCode() {
  return defHttp.get<string[]>({ url: Api.GetPermCode })
}

export function doLogout() {
  return defHttp.post({ url: Api.Logout })
}

export function testRetry() {
  return defHttp.get(
    { url: Api.TestRetry },
    {
      retryRequest: {
        isOpenRetry: true,
        count: 5,
        waitTime: 1000,
      },
    },
  )
}
